var companyId;
//绑定银行信用卡的URL
//var bindingBankURL = "http://58.56.12.30:8081/v1.0/channel/bindPayCard";
var idcardimgSuc = false;	//手持身份证照片上传失败
var doorheaderimgSuc = false;	//门口照片上传失败
$(function () {
    companyId = $.getUrlParam("id");
    //加载日期控件
    laydate.render({
    	elem: '#comnumRegisterTime',
    	type: 'date'
    });
    
    //上传图片的路径
//    var path = "../company/imageUpload.htm";
    //初始化上传图片组件
//    initFileInput("idcardimg", path);
//    initFileInput("doorheaderimg", path);
    
    //是否验证成功
    $('#companyForm').validationEngine('attach', {
        validationEventTrigger : '',	//当提交的时候验证是否合法
        promptPosition: 'topRight',
        scroll: false,
        focusFirstField : true,
        onValidationComplete : function (form, valid) {
            if (valid){
                if (companyId == 'null') {
                	addCompanyInfo();
                } else {
                	modifyCompanyInfo();
                }
            }
        }
    });
    
    $("#map").click(function(){
    	var index = layer.open({
            type: 2,
            title: '获取详细地址',
            maxmin: true,
            shadeClose: false, //点击遮罩关闭层
            area : ['500px' , '750px'],
            content: '../company/map_company.htm',
            success: function(layero, index){
            }
        });
        layer.full(index);
    	
    });
    
    //如果company为null，则是增加操作，否则是编辑操作.
    //这个地方使用的return，后面的语句将执行不到
    if(companyId == 'null'){
    	return;
    }else{
    	echoCompanyInfo();
    	$("#comnumIdCardImgName").val("");
    	$("#comnumDoorHeaderImgName").val("");
    }
});

//点击编辑回显数据
function echoCompanyInfo(){
    $.ajax({
        url: "../company/getCompanyInfoById.htm",
        cache: false,
        async : false,
        type: "post",
        dataType: "json",
	    data: {companyId: companyId},
        success: function (result) {
            if (result.status == 0) {
                var data = result.data;
                $("#companyForm").formatForm(data);
                return;
            }
        }
    });
}

//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

//修改
function modifyCompanyInfo(){
    $.ajax({
        url: "../company/modifyCompanyInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data : $("#companyForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../company/getCompanyPage.htm'});
                closeForm();
            } else {
                parent.layer.msg(result.message, {icon: 2});
            }
        }
    });
}

//增加
function addCompanyInfo() {
    $.ajax({
        url: "../company/addToCompanyInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: $("#companyForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../company/getCompanyPage.htm'});
                closeForm();
            } else {
                parent.layer.msg(result.message, {icon: 2});
            }
        }
    });
}

//上传图片组件
function initFileInput(ctrlName, uploadUrl){
	var control = $('#' + ctrlName);
	control.fileinput({
		uploadUrl: uploadUrl,	//上传地址
		uploadAsync : true, //默认异步上传
		showUpload : false, //是否显示上传按钮,跟随文本框的那个
		showRemove : false, //显示移除按钮,跟随文本框的那个
		showCaption : true,//是否显示标题,就是那个文本框
		showPreview : true, //是否显示预览,不写默认为true
		dropZoneEnabled : false,//是否显示拖拽区域，默认不写为true，但是会占用很大区域
		maxFileCount : 1, //表示允许同时上传的最大文件个数
		enctype : 'multipart/form-data',
		validateInitialCount : true,
		previewFileIcon : "<i class='glyphicon glyphicon-king'></i>",
		msgFilesTooMany : "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
		allowedFileTypes : [ 'image' ],//配置允许文件上传的类型
		allowedPreviewTypes : [ 'image' ],//配置所有的被预览文件类型
		allowedPreviewMimeTypes : [ 'jpg', 'png', 'gif', 'jpeg' ],//控制被预览的所有mime类型
		language : 'zh' 
	}).on("filepreupload", function(event, data, previewId, index){				//上传前
		 
	}).on("fileuploaded", function(event, data, previewId, index){	 //上传图片后的回调函数，可以在这做一些处理
		var ref = $(this).attr("data-ref");
		$("input[name='" + ref + "']").val(data.response.data);
		//如果status 为1 则页面提示上传失败
		if(data.response.status == 1){
			parent.layer.msg(data.response.message, {icon: 2});
		}else if(data.response.status == 0){	//如果是上传成功，则设置上传成功标示符
			if(ctrlName == "idcardimg"){
				idcardimgSuc = true;
			}else {
				doorheaderimgSuc = true;
			}
		}
	}).on("fileerror", function(event, data, msg){
		console.log("fileupload error..............");
	});
}


//salesmanComnum的下拉列表  追加到option中
function addOptions(data, selectId){
	for(var i=0; i<data.length; i++){
		$(selectId).append('<option value="' + data[i].comnumNum + '">' + data[i].comnumName + '</option>');
	}
}